package com.cooksys.film_rental.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cooksys.film_rental.dao.CategoryDao;
import com.cooksys.film_rental.model.Category;
import com.cooksys.film_rental.model.Film;

@Repository("CategoryDao")
@Transactional
public class CategoryDaoImpl implements CategoryDao {

	@Autowired(required = true)
	private SessionFactory sessionFactory;

	/**
	 * 
	 * @return the current session from the internal {@link SessionFactory}
	 */
	public Session currentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public Category getCategoryByCategoryId(Byte categoryId) {
		Session session = currentSession();

		// returns a record from the CATEGORY table as a Category object based
		// on the given categoryId, the table's primary key
		String hql = "from Category c where c.categoryId = :categoryId";
		Query query = session.createQuery(hql);
		query.setByte("categoryId", categoryId);

		@SuppressWarnings("rawtypes")
		List results = query.list();
		if (results.size() == 0) {
			return null;
		}
		return (Category) results.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> searchCategoriesByPartialName(String partialName) {

		Session session = currentSession();

		// split the search string into search terms
		String[] nameFragments = partialName.split("\\s");

		// 'select' statement of the hql query
		String hql = "from Category c where ";

		// generating the where clause
		for (int i = 0; i < nameFragments.length; i++) {

			// for each search term, add a segment of the where clause that
			// checks the name column of the CATEGORY table for matches
			hql += "c.name like :nameFragment" + i;

			if (i < nameFragments.length - 1) {

				// concatenate these where clause segments with an 'or' operator
				hql += " or ";

			}

		}

		Query query = session.createQuery(hql);

		for (int i = 0; i < nameFragments.length; i++) {

			// parameterize the query with the search terms
			query.setString("nameFragment" + i, "%" + nameFragments[i] + "%");

		}

		return query.list();

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> searchCategoriesByFilm(Film film) {
		Session session = currentSession();

		// selects the category objects associated with a given film object
		String hql = "select f.category from FilmCategory f where f.film = :film";
		Query query = session.createQuery(hql);
		query.setEntity("film", film);

		return query.list();
	}

	@Override
	public List<Category> getAllCategories() {
		Session session = currentSession();

		String hql = "from Category";
		Query query = session.createQuery(hql);

		return query.list();
	}

}
